package oop.factor;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import oop.data.RowData;


/**
 * 读取文件中的数据，按行存储
 */
public class Reader {
    
    private File File;
    private List<RowData> datas;
    
    public Reader(String path) {
        File = new File(path);
    }
    
    public Integer size() {
        return datas.size();
    }
    
    public Iterator<RowData> getIterator() {
        //第一次获取仓库数据时初始化仓库数据
        if (datas == null) {
            initDatas();
        }
        return datas.iterator();
    }
    
    public String getDescribe() {
        return File.getName();
    }
    public List<RowData> getDatas() {
        if (datas == null) {
            initDatas();
        }
        return datas;
    }
    private void initDatas() {
        datas = new ArrayList<RowData>();
        try {
            FileInputStream in = new FileInputStream(File);
            InputStreamReader inReader = new InputStreamReader(in, "UTF-8");
            BufferedReader bufReader = new BufferedReader(inReader);
            
            
            String line = null;
            
            String[] columnArray = null;
            String[] dataArray = null;
            Map<String, String> mapData = null;
            
            line = bufReader.readLine();
            if (line != null) {
            	//columnArray 行标
                columnArray = line.split(",");
            }
            
            System.out.println("Reading the " + File.getName() + ":");
            while ((line = bufReader.readLine()) != null) {
                //忽略空行
                String pattern = "\\s+";
                boolean isBlank = Pattern.matches(pattern, line);
                if (line.equals("") || isBlank) {
                    System.out.println("\t there has none-line!");
                    continue;
                }
                
                
                mapData = new HashMap<String, String>();
                
                dataArray = line.split(",");
                
                try {
                    for (int i = 0; i < columnArray.length; i++) {
                        //最后一列是空的情况单独处理
                        if (i == columnArray.length - 1 && dataArray.length == columnArray.length - 1) {
                            mapData.put(columnArray[i], "");
                            continue;
                        }
                        mapData.put(columnArray[i], dataArray[i]);
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
              
                String fatherName = "";
        		Pattern pattern1 = Pattern.compile("(.*)\\.csv");
        		Matcher matcher1 = pattern1.matcher(File.getName());
        		while(matcher1.find()){
        			fatherName = matcher1.group(1); 
        		}

                // 存储
                datas.add(new RowData(fatherName,columnArray, mapData));
            }
            bufReader.close();
            inReader.close();
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("读取出错！");
        }
    }
    
    

}
